package com.swu.pad.activity.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swu.pad.activity.dao.ActivityDao;
import com.swu.pad.activity.dao.ActivityGroupVisionDao;
import com.swu.pad.activity.entity.Activity;
import com.swu.pad.activity.entity.ActivityGroupVision;
import com.swu.pad.activity.service.ActivityService;
import com.swu.pad.assignment.dao.AssignmentDao;
import com.swu.pad.assignment.dao.AssignmentPadDao;
import com.swu.pad.assignment.entity.Assignment;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.assignment.service.AssignmentService;
import com.swu.pad.comment.dao.CommentQuestionDao;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.course.dao.CourseGroupUserDao;
import com.swu.pad.course.dao.GroupDao;
import com.swu.pad.course.entity.Course;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.course.entity.Group;
import com.swu.pad.course.service.CourseService;
import com.swu.pad.util.StringUtil;
import com.swu.pad.util.EPLiteClient.EPLiteClient;

@Service
@Transactional
public class ActivityServiceImpl implements ActivityService {

	@Autowired
	private GroupDao groupDao;
	@Autowired
	private AssignmentDao assignmentDao;
	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private CourseGroupUserDao courseGroupUserDao;
	@Autowired
	private ActivityGroupVisionDao activityGroupVisionDao;
	@Autowired
	private AssignmentPadDao assignmentPadDao;
	@Autowired
	private CommentQuestionDao commentQuestionDao;
	@Autowired
	private CourseService courseService;
	@Autowired
	private AssignmentService assignmentService;
	@Autowired
	private EPLiteClient epliteClient;

	public Activity createActivity(Activity activity) {
		activity = activityDao.saveAndFlush(activity);
		List<CommentQuestion> questionList = commentQuestionDao.findByTemplateId(activity.getCommentTemplateId());
		Map<String, Object> templateMap = new HashMap<String, Object>();
		if (questionList != null && questionList.size() > 0) {
			for (int i = 0; i < questionList.size(); i++) {
				templateMap.put(questionList.get(i).getTitle(), questionList.get(i).getQuestion());
			}
		}
		Assignment assignment = assignmentDao.findOne(activity.getAssignmentId());
		List<Group> groupList = null;
		if (Assignment.TYPE_SINGLE.equals(assignment.getAssignmentType())) {
			groupList = groupDao.findGroupByCourseIdAndGroupType(assignment.getCourseId(), Group.TYPE_SINGLE);
		} else {
			groupList = groupDao.findGroupByCourseIdAndGroupType(assignment.getCourseId(), Group.TYPE_GROUP);
		}
		List<ActivityGroupVision> activityGroupVisionList = getActivityGroupVisions(activity.getId());
		for (int i = 0; i < groupList.size(); i++) {
			if (activityGroupVisionList == null || activityGroupVisionList.isEmpty()) {
				ActivityGroupVision activityGroupVision = new ActivityGroupVision();
				activityGroupVision.setActivityId(activity.getId());
				activityGroupVision.setGroupId(groupList.get(i).getId());
				activityGroupVision.setGroupName(groupList.get(i).getGroupName());
				activityGroupVision.setGroupType(groupList.get(i).getGroupType());
				activityGroupVisionDao.saveAndFlush(activityGroupVision);
			}
			if (!templateMap.isEmpty()) {
				AssignmentPad assignmentPad = assignmentPadDao.findAssignmentPadByAssignmentIdAndGroupId(assignment.getId(), groupList.get(i).getId());
				epliteClient.createCommentTemplate(assignmentPad.getId(), templateMap);
			}
		}
		return activity;
	}

	public List<Activity> getActivityByAssignmentId(String assignmentId) {
		return activityDao.findActivityByAssignmentId(assignmentId);
	}

	public void deleteActivity(String activityId) {
		activityDao.delete(activityId);
	}

	public List<ActivityGroupVision> getActivityGroupVisions(String activityId) {
		Activity activity = activityDao.findOne(activityId);
		Assignment assignment = assignmentDao.findOne(activity.getAssignmentId());
		if (Assignment.TYPE_SINGLE.equals(assignment.getAssignmentType())) {
			return activityGroupVisionDao.findActivityGroupVisionByActivityIdAndGroupType(activityId, Group.TYPE_SINGLE);
		} else {
			return activityGroupVisionDao.findActivityGroupVisionByActivityIdAndGroupType(activityId, Group.TYPE_GROUP);
		}
	}

	public ActivityGroupVision saveActivityGroupVision(ActivityGroupVision activityGroupVision) {
		return activityGroupVisionDao.saveAndFlush(activityGroupVision);
	}

	public Activity getAssignmentCurrentActivity(String assignmentId) {
		Date date = new Date();
		List<Activity> activityList = activityDao.findByAssignmentIdAndStartDateLessThanEqualAndEndDateGreaterThan(assignmentId, date, date);
		if (activityList != null && activityList.size() > 0)
			return activityList.get(0);
		return null;
	}

	public List<Map<String, Object>> getAllCurrentActivity(String userId) {
		// [{id:'',activityName:'',activityType:'',description:'',startDate:'',endDate:'',assignmentId:'',assignmentName,courseId:'',courseName:''}]
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Course> courseList = courseService.getStudentCourse(userId);
		for (int i = 0; i < courseList.size(); i++) {
			List<Assignment> assignmentList = assignmentService.getAssignmentByCourseId(courseList.get(i).getId());
			for (int j = 0; j < assignmentList.size(); j++) {
				Date date = new Date();
				List<Activity> activityList = activityDao.findByAssignmentIdAndStartDateLessThanEqualAndEndDateGreaterThan(assignmentList.get(j).getId(), date, date);
				if (activityList != null && activityList.size() > 0) {
					Activity activity = activityList.get(0);
					Map<String, Object> tempMap = new HashMap<String, Object>();
					tempMap.put("id", activity.getId());
					tempMap.put("activityName", activity.getActivityName());
					tempMap.put("activityType", activity.getActivityType());
					tempMap.put("description", activity.getDescription());
					tempMap.put("startDate", activity.getStartDate());
					tempMap.put("endDate", activity.getEndDate());
					tempMap.put("assignmentId", assignmentList.get(j).getId());
					tempMap.put("assignmentName", assignmentList.get(j).getAssignmentName());
					tempMap.put("courseId", courseList.get(i).getId());
					tempMap.put("courseName", courseList.get(i).getCourseName());
					list.add(tempMap);
				}
			}
		}
		return list;
	}

	public Map<String, Object> getCommentAssignmentPad(String activityId, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<AssignmentPad> list = new ArrayList<AssignmentPad>();
		Activity activity = activityDao.findOne(activityId);
		Assignment assignment = assignmentDao.findOne(activity.getAssignmentId());
		CourseGroupUser courseGroupUser = null;
		if (Assignment.TYPE_SINGLE.equals(assignment.getAssignmentType())) {
			courseGroupUser = courseGroupUserDao.findCourseGroupUserByCourseIdAndUserIdAndGroupType(assignment.getCourseId(), userId, Group.TYPE_SINGLE);
		} else {
			courseGroupUser = courseGroupUserDao.findCourseGroupUserByCourseIdAndUserIdAndGroupType(assignment.getCourseId(), userId, Group.TYPE_GROUP);
		}
		Group group = groupDao.findOne(courseGroupUser.getGroupId());
		String commentGroups = group.getCommentGroups();
		if (StringUtil.isValid(commentGroups)) {
			String[] commentGroupArr = commentGroups.split(":");
			for (int i = 0; i < commentGroupArr.length; i++) {
				Group commentGroup = groupDao.findByCourseIdAndGroupTypeAndGroupNum(group.getCourseId(), group.getGroupType(), commentGroupArr[i]);
				AssignmentPad assignmentPad = assignmentService.getUserAssignmentPad(assignment.getId(), commentGroup.getId());
				list.add(assignmentPad);
			}
		}
		map.put("assignmentPads", list);
		return map;
	}

	public ActivityGroupVision getActivityGroupVision(String activityId, String userId) {
		Activity activity = activityDao.findOne(activityId);
		Assignment assignment = assignmentDao.findOne(activity.getAssignmentId());
		if (Assignment.TYPE_SINGLE.equals(assignment.getAssignmentType())) {
			CourseGroupUser courseGroupUser = courseGroupUserDao.findCourseGroupUserByCourseIdAndUserIdAndGroupType(assignment.getCourseId(), userId, Group.TYPE_SINGLE);
			return activityGroupVisionDao.findActivityGroupVisionByActivityIdAndGroupId(activityId, courseGroupUser.getGroupId());
		} else {
			CourseGroupUser courseGroupUser = courseGroupUserDao.findCourseGroupUserByCourseIdAndUserIdAndGroupType(assignment.getCourseId(), userId, Group.TYPE_GROUP);
			return activityGroupVisionDao.findActivityGroupVisionByActivityIdAndGroupId(activityId, courseGroupUser.getGroupId());
		}
	}

}
